import CommonHeader from '@/components/common-header/index'
import SelectMerit from '@/apis/SelectMerit'
export default {
  name: 'PerformanceShow',
  components: {
    CommonHeader,
  },
  mounted() {
    SelectMerit(
      {
        jobNumber: this.$store.state.userInfo.jobNumber,
        startDate: '2020-1',
        endDate: new Date().getFullYear() + '-' + (new Date().getMonth() + 1),
      },
      this.$store.state.authorization
    ).then((res) => {
      console.log(res)
      if (res.data.flag) {
        this.performanceList = res.data.data
      } else {
        this.performanceList = []
      }
    })
  },
  onload() {
    // 兼容实际环境
    this.minDate = new Date('2020-1').getTime()
  },
  data() {
    return {
      // 日期选择器
      type: 'begin',
      ifDateShow: false,
      currentDate: '',
      dateBegin: '开始月份',
      dateEnd: '结束月份',
      maxDate: new Date().getTime(),
      minDate: new Date('2020-1').getTime(),
      // 查询绩效
      performanceList: [],
    }
  },
  methods: {
    closeDate() {
      this.ifDateShow = false
    },
    DateConfirm(event) {
      const value = event.detail
      const year = new Date(value).getFullYear()
      const month =
        new Date(value).getMonth() + 1 <= 9
          ? '0' + (new Date(value).getMonth() + 1)
          : (new Date(value).getMonth() + 1).toString()
      if (this.type === 'begin') {
        this.dateBegin = year + '-' + month
        this.minDate = new Date(this.dateBegin).getTime()
      } else {
        this.dateEnd = year + '-' + month
        this.maxDate = new Date(this.dateEnd).getTime()
      }
      if (this.dateBegin !== '开始月份' && this.dateEnd !== '结束月份') {
        SelectMerit(
          {
            jobNumber: this.$store.state.userInfo.jobNumber,
            startDate: this.dateBegin,
            endDate: this.dateEnd,
          },
          this.$store.state.authorization
        ).then((res) => {
          // console.log(res)
          if (res.data.flag) {
            this.performanceList = res.data.data
          } else {
            this.performanceList = []
          }
        })
      }
      this.ifDateShow = false
    },
    DateCancel() {
      this.ifDateShow = false
    },
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return `${value}日`
    },
    selectBegin() {
      this.type = 'begin'
      this.ifDateShow = true
      this.minDate = new Date('2018-1').getTime()
    },
    selectEnd() {
      this.type = 'end'
      this.ifDateShow = true
      this.maxDate = new Date().getTime()
    },
  },
}
